//
//  GrowDetailViewController.m
//  XRSquare
//
//  Created by 穆相臣 on 2018/11/2.
//  Copyright © 2018 UaenaMo. All rights reserved.
//

#import "GrowDetailViewController.h"

@interface GrowDetailViewController ()
{
    UILabel * titleLabel;
    UILabel * typeLabel;
    UILabel * timeLabel;
    UILabel * timeLabel1;
    UILabel * contentLabel;
    UIView * contentImageView;
    NSArray * imageArr;
    
    UIScrollView * scrollView;
}
@end

@implementation GrowDetailViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        imageArr = [NSArray array];
    }
    return self;
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)requestData
{
    [NetRequestCenter archivedetailTaskId:self.contentId Result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            NSLog(@"%@",resultDic);
            titleLabel.text = [[resultDic objectForKey:@"data"]objectForKey:@"title"];
            typeLabel.text = [NSString stringWithFormat:@"%@",[[resultDic objectForKey:@"data"]objectForKey:@"type"]];
            timeLabel.text = [NSString stringWithFormat:@"发布时间:%@",[[resultDic objectForKey:@"data"]objectForKey:@"publishTime"]];
            timeLabel1.text = [NSString stringWithFormat:@"记录时间:%@",[[resultDic objectForKey:@"data"]objectForKey:@"eventTime"]];
            contentLabel.text = [NSString stringWithFormat:@"%@",[[resultDic objectForKey:@"data"]objectForKey:@"upContent"]];
            [contentLabel sizeToFit];
            imageArr = [[resultDic objectForKey:@"data"]objectForKey:@"picUrls"];
            contentImageView.frame = CGRectMake(0, contentLabel.frame.size.height + contentLabel.frame.origin.y + 10, ScreenWidth, [imageArr count]/4*(ScreenWidth/4) + ScreenWidth/4);
            [self loadImage];
            
            scrollView.contentSize = CGSizeMake(ScreenWidth, contentImageView.frame.origin.y + contentImageView.frame.size.height);
        }
    }];
}

-(void)loadImage
{
    for (int i = 0; i < [imageArr count]; i ++)
    {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/4 * (i%4) + 5, ScreenWidth/4 * (i/4) + 5,ScreenWidth/4 - 10, ScreenWidth/4 - 10)];
        NSString * imageUrl = [NSString stringWithFormat:@"%@",[imageArr objectAtIndex:i]];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [contentImageView addSubview:imageView];
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self layoutContent];
    [self requestData];
}

-(void)layoutContent
{
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth, ScreenHeight - rectStatus.size.height - rectNav.size.height)];
    [self.view addSubview:scrollView];
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 20)];
    titleLabel.font = [UIFont systemFontOfSize:17];
    [scrollView addSubview:titleLabel];
    
    typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, ScreenWidth - 20, 20)];
    typeLabel.font = [UIFont systemFontOfSize:15];
    typeLabel.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
    [scrollView addSubview:typeLabel];
    
    timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, ScreenWidth - 20, 20)];
    timeLabel.font = [UIFont systemFontOfSize:15];
    timeLabel.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
    [scrollView addSubview:timeLabel];

    timeLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 120, ScreenWidth - 20, 20)];
    timeLabel1.font = [UIFont systemFontOfSize:15];
    timeLabel1.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
    [scrollView addSubview:timeLabel1];
    
    contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 160, ScreenWidth - 20, 20)];
    contentLabel.font = [UIFont systemFontOfSize:17];
    contentLabel.numberOfLines = 0;
    [scrollView addSubview:contentLabel];
    
    contentImageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth)];
    [scrollView addSubview:contentImageView];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImages)];
    [contentImageView addGestureRecognizer:tap];
}

-(void)showImages
{
    MYPhotoBrowser *photoBrowser = [[MYPhotoBrowser alloc]initWithUrls:imageArr imgViews:nil placeholder:nil currentIdx:0 handleNames:nil callback:^(UIImage *handleImage,NSString *handleType) {
        
    }];
    [photoBrowser showWithAnimation:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
